package com.moli.iov.down.command.codec.encode.data;

import com.moli.iov.down.enums.command.upgrade.OvertimeOperationEnum;
import com.moli.iov.down.enums.command.upgrade.UpgradeConditionsEnum;
import com.moli.iov.down.enums.command.upgrade.UpgradeMarkEnum;
import com.moli.iov.down.enums.command.upgrade.UpgradeTargetEnum;
import com.moli.iov.down.model.dto.remoting.CommandDto;
import com.moli.iov.util.ByteArrayUtil;
import org.apache.commons.lang.ArrayUtils;

/**
 * <p>
 *  恒领远程升级命令编码
 * </p>
 *
 * @author: zql
 * @Date: 2019/4/8 14:34
 */
public class UpgradeEncoder extends BaseEncoder implements DataEncoder  {


    @Override
    public byte[] encode(CommandDto commandDto) {

        byte[] all = getByteBuf(commandDto);

        byte[] url = ByteArrayUtil.strToAscii(commandDto.getUrl());

        byte[] taget = {UpgradeTargetEnum.TERMINAL.getFlag(),
                        UpgradeConditionsEnum.CONDITION_ONE.getFlag(),
                        UpgradeMarkEnum.MANDATORY.getFlag(),
                        OvertimeOperationEnum.NO_UPGRADE.getFlag(),
                        ByteArrayUtil.toByte(url.length)};

        all = ArrayUtils.addAll(all, taget);

        return ArrayUtils.addAll(all, url);
    }
}
